/**
 * Program   :   LogOutFilter.java
 * Author    :   shadow guo
 * Create    :   2013-6-15 下午9:38:52
 *
 * Copyright 2008 by Tongfu Information Technology Co. Ltd
 * All rights reserved.
 *
 * This software is the confidential and proprietary information
 * of Tongfu Information Technology Co. Ltd. ("Confidential Information").  You
 * shall not disclose such Confidential Information and shall use
 * it only in accordance with the terms of the license agreement
 * you entered into with Tongfu Information Technology Co. Ltd.
 * 
 */
package com.tongfusoft.web.core.security;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.security.core.Authentication;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;

import com.tongfusoft.utils.UIDGeneratorService;
import com.tongfusoft.web.core.data.TLogs;
import com.tongfusoft.web.core.data.User;
import com.tongfusoft.web.core.service.ILogService;
import com.tongfusoft.web.core.service.ISecurityService;


public class BaseLogoutHandler implements LogoutSuccessHandler {
	/**
	 * Logger for this class
	 */
	private static final Log logger = LogFactory.getLog(BaseLogoutHandler.class);

	private ILogService logService;
	private ISecurityService securityService;
	
	
	public ISecurityService getSecurityService() {
		return securityService;
	}

	public void setSecurityService(ISecurityService securityService) {
		this.securityService = securityService;
	}

	public ILogService getLogService() {
		return logService;
	}

	public void setLogService(ILogService logService) {
		this.logService = logService;
	}

	/*public void logout(HttpServletRequest request,
			HttpServletResponse response, Authentication authentication) {
		User user = (User)request.getSession().getAttribute(Constant.SESSION_USER);
		TLogs log = this.getLog(user);
		this.getLogService().log(log);
	}*/
	
	private TLogs getLog(User user){
		TLogs tLog = new TLogs();
		tLog.setLogId(UIDGeneratorService.getUUID());
		tLog.setCreatedBy(user.getPkValue());
		tLog.setOperName(user.getUserDisplayName());
		tLog.setOperType("logout");
		tLog.setMethodName(this.getClass().getName()+".logout");
		tLog.setLogContent("退出系统");
		tLog.setOperResult("1"); //成功
		tLog.setParamsContent("");
		
		return tLog;
	}

	@Override
	public void onLogoutSuccess(HttpServletRequest request,
			HttpServletResponse response, Authentication authentication)
			throws IOException, ServletException {
		try{
			String userName = ((UserDetails)authentication.getPrincipal()).getUsername();
			
			TLogs log = this.getLog(this.getSecurityService().getUserInfo(userName));
			this.getLogService().log(log);
		}catch(Exception e){
			logger.error(e);
		}
		
		//跳转初始页面
	    response.sendRedirect(request.getContextPath());
	}

}
